package com.example.boot.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.Result;
import com.example.common.enums.KeyEnum;
import com.example.common.websocket.WebSocketServe;
import com.example.context.BaseContext;
import com.example.mapper.AdminMapper;
import com.example.pojo.entity.Admin;
import com.example.pojo.entity.Notice;
import com.example.pojo.entity.Notify;
import com.example.pojo.vo.NotifyVO;
import com.example.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author ding
 * @since 2024/7/2
 */
@Api(tags = "通知接口")
@RestController
@RequestMapping("/notify")
public class NotifyController {

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private WebSocketServe webSocketServe;

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 获取通知信息 公告 待审批业务
     */
    @ApiOperation("获取通过信息")
//    @PreAuthorize("hasAuthority('NOTIFY_INFO')")
    @GetMapping("/getAll")
    public Result getNotifyList(){
        List<Notice> noticeList = noticeService.list(new LambdaQueryWrapper<Notice>().eq(Notice::getIsDeleted, 0));
        List<Notify> notifyList1 = new ArrayList<>();
        for (Notice notice : noticeList) {
            Notify notify  = new Notify();
            BeanUtils.copyProperties(notice, notify);
            notifyList1.add(notify);
        }
        NotifyVO notifyVO1 = NotifyVO.builder()
                .key("1")
                .name("公告")
                .list(notifyList1)
                .build();

        NotifyVO[] notifyVOS = new NotifyVO[1];
        notifyVOS[0] = notifyVO1;

        //当前在线人id   查看其是否有待审批业务
        Integer userId = Math.toIntExact(BaseContext.getCurrentId());
        Long size = redisTemplate.opsForSet().size(KeyEnum.TASK + "_" + userId);
        if (size != null && size > 0) {
            Map wbMap = new HashMap();
            wbMap.put("type", 3);
            Admin admin = adminMapper.selectOne(new LambdaQueryWrapper<Admin>()
                                     .select(Admin::getName)
                                     .eq(Admin::getId, userId));
            wbMap.put("online", admin.getName());
            wbMap.put("size", size);
            String json = JSON.toJSONString(wbMap);
            webSocketServe.sendToClient(json, userId + "");

        }

        return Result.success(notifyVOS);
    }

}

